<?php

namespace app\api\controller\v1;

use app\admin\model\Bizperson;
use app\admin\model\Customer;
use app\admin\model\Porder;
use app\admin\model\Wxuser;

class Userinfo extends Base
{
    //完善信息
    public function perfectMessage()
    {
        $check      = $this->plulicValidate($this->params,
            ['fk_bdl_reqcompanyname|注册公司名称', 'fk_bdl_truename|真实名称', 'fk_bdl_requsername|注册联系人', 'fk_bdl_reqaddress|注册通讯地址', 'fk_bdl_reqphone|注册联系电话'],
            ['require',  'require',  'require',  'require',  'require']);

        if ($check['code'] == 1)
        {
            $this->error($check['msg']);
        }

        $params         = $this->params;
        $params['id']   = $this->user_id;

        $user   = new Wxuser();

        $res    = $user->isUpdate(true)->save($params);


        if($res)
        {
            $this->success('完善成功');
        }

        $this->error('请稍后再试');

    }

    //获取用户信息
    public function getUserInfo()
    {
        $data = Wxuser::get($this->user_id);

        if(isset($data['fk_bdl_imageUrl']) && !empty($data['fk_bdl_imageUrl']))
        {
            $data['avatar']  = $data['fk_bdl_imageUrl'];
        }else{
            $data['avatar']  = now_domain().'/static/mini_program/common_bdl_logo.png';
        }

        //是否完善信息
        if($data['fk_bdl_reqcompanyname'] !== ' ' && $data['fk_bdl_requsername'] !== ' ' && $data['fk_bdl_reqaddress'] !== ' '&& $data['fk_bdl_reqphone'] !== ' '&& $data['fk_bdl_reqattachurl'] !== ' ')
        {
            $data->is_perfect = 1;
        }else{
            $data->is_perfect = 2;
        }


        $this->success('',$data);
    }

    public function index()
    {
        $statusCounts = [
            1 => 0,
            2 => 0,
            3 => 0,
        ];

        if($this->customer_id){
            // 遍历每个状态，并查询数量
            foreach ([1, 2, 3] as $status) {
                $count = Porder::where('fk_bdl_orderstatus', $status)
                    ->where('fk_bdl_bd_customer', $this->customer_id)
                    ->count();
                $statusCounts[$status] = $count;
            }

            $cus = Customer::get($this->customer_id);
            $biz = Bizperson::get($cus['fk_bdl_bd_salesperson']);
        }else{
            $biz = [];
        }



        $this->success('',[
            'Jcount'    => $statusCounts,
            'biz'       => $biz,
        ]);

    }

}